package com.team.weigou.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.etsy.android.grid.StaggeredGridView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshStaggeredGridView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.team.weigou.R;
import com.team.weigou.adapter.HotInfoAdapter;
import com.team.weigou.bean.HotinfoData;
import com.team.weigou.config.UrlConfig;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Wangs on 2015/7/10.
 */
public class SearchActivity extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener {
    @ViewInject(R.id.search_edit)
    private EditText search_edit;
    @ViewInject(R.id.search_img_cancle)
    private ImageView search_img_cancle;

    @ViewInject(R.id.search_img_clean)
    private ImageView search_img_clean;


    //自定义下拉刷新瀑布流

    private HttpUtils httpUtils = new HttpUtils();
    private BitmapUtils bitmapUtils;

    //自定义下拉刷新瀑布流
    private PullToRefreshStaggeredGridView prrsgv;
    private StaggeredGridView staggeredGridView;
    private HotInfoAdapter adapter;
    private ArrayList<HotinfoData> list = new ArrayList<>();
    private boolean isClear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_layout);
        ViewUtils.inject(this);
        initBitmUtils();
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        prrsgv = (PullToRefreshStaggeredGridView) findViewById(R.id.footer_rebate_gridview);
        prrsgv.setMode(PullToRefreshBase.Mode.BOTH);
        staggeredGridView = prrsgv.getRefreshableView();
        adapter = new HotInfoAdapter(this, list, bitmapUtils);
        prrsgv.setRefreshing();
        prrsgv.setAdapter(adapter);
        return super.onCreateView(parent, name, context, attrs);
    }

    /**
     * 初始化BitmapUtils工具类
     */
    private void initBitmUtils() {
        bitmapUtils = new BitmapUtils(this);
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_empty);
        bitmapUtils.configDefaultLoadingImage(R.mipmap.ic_empty);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_edit:
                if (search_edit.getText() == null) {
                    search_img_cancle.setImageResource(R.mipmap.search_cancle);
                    search_img_cancle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            SearchActivity.this.finish();
                        }
                    });
                } else {
                    search_img_cancle.setImageResource(R.mipmap.search_sure);
                    search_img_cancle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            initData();
                        }
                    });
                }
                break;
            case R.id.search_img_clean:
                search_edit.setText("");
                break;
        }
    }

    private void initData() {
        getDataFromServer(UrlConfig.HOT_INFO_URL, adapter, list, prrsgv, isClear);

    }

    public void getDataFromServer(String url, final BaseAdapter adapter, final List<HotinfoData> list, final PullToRefreshBase pullToRefreshBase, final boolean isClear) {
        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        try {
                            String json = responseInfo.result;
                            String msg = new JSONObject(json).optString("msg");
                            if ("获取成功".equals(msg)) {
                                JSONArray arr = new JSONObject(json).getJSONArray("data");
                                List<HotinfoData> datas = JSON.parseArray(arr.toString(), HotinfoData.class);
                                if (isClear) {
                                    list.clear();
                                }
                                list.addAll(datas);
                                adapter.notifyDataSetChanged();
                                pullToRefreshBase.onRefreshComplete();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                    }
                }
        );
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, GoodsActivity.class);
        intent.putExtra("shopId", list.get(position).getShopId());
        intent.putExtra("name", list.get(position).getName());
        intent.putExtra("price",list.get(position).getPrice());
        intent.putExtra("value", list.get(position).getValue());
        intent.putExtra("image", list.get(position).getImage());
        startActivity(intent);
    }

}
